package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EquipmentSafetyProtective;
import com.ruoyi.system.entity.SysRemind;
import com.ruoyi.system.mapper.EquipmentSafetyProtectiveMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IEquipmentSafetyProtectiveService;
import com.ruoyi.system.service.SysRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EquipmentSafetyProtectiveServiceImpl implements IEquipmentSafetyProtectiveService {

    @Autowired
    private SysRemindService sysRemindService;
    @Resource
    private EnterpriseWXServiceImpl enterpriseWXService;
    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private EquipmentSafetyProtectiveMapper equipmentSafetyProtectiveMapper;

    @Override
    public List<EquipmentSafetyProtective> selectEquipmentSafetyProtectiveList(EquipmentSafetyProtective request) {
        return equipmentSafetyProtectiveMapper.selectEquipmentSafetyProtectiveList(request);
    }

    @Override
    public int saveEquipmentSafetyProtective(EquipmentSafetyProtective request) {
        return equipmentSafetyProtectiveMapper.saveEquipmentSafetyProtective(request);
    }

    @Override
    public int editEquipmentSafetyProtective(EquipmentSafetyProtective request) {
        return equipmentSafetyProtectiveMapper.editEquipmentSafetyProtective(request);
    }

    @Override
    public void deleteEquipmentSafetyProtective(Long[] ids) {
        for (Long configId : ids) {

            EquipmentSafetyProtective request = new EquipmentSafetyProtective();
            request.setId(configId.intValue());
            equipmentSafetyProtectiveMapper.deleteEquipmentSafetyProtective(request);

        }

    }

    @Override
    public String importExcel(List<EquipmentSafetyProtective> equipmentSafetyProtectives, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(equipmentSafetyProtectives) || equipmentSafetyProtectives.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }

        for (EquipmentSafetyProtective value : equipmentSafetyProtectives) {
            equipmentSafetyProtectiveMapper.saveEquipmentSafetyProtective(value);
        }

        return "成功";
    }

    @Scheduled(cron = "0 0 8 * * ?")
    public void checkEquipmentSafetyProtective() {

        EquipmentSafetyProtective equipmentSafetyProtective = new EquipmentSafetyProtective();
        List<EquipmentSafetyProtective> equipmentSafetyProtectives = equipmentSafetyProtectiveMapper.selectEquipmentSafetyProtectiveList(equipmentSafetyProtective);
        if (CollectionUtils.isEmpty(equipmentSafetyProtectives)) {
            return;
        }

        for (EquipmentSafetyProtective var : equipmentSafetyProtectives) {
            String remind = var.getNextCheckTime();
            Date date = new Date();

            Date parseDate = DateUtils.parseDate(remind);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(parseDate);
            // 把日期往后增加一天,整数  往后推,负数往前移动
            calendar.add(Calendar.DATE, -1);

            boolean after = date.after(calendar.getTime());
            if (after) {
                String stringBuffer = "设备安全防护设施 设备名称:" + var.getEquipmentName() + "即将到期。";

               /* ViewConfigPushMes viewConfigPushMes = new ViewConfigPushMes();
                viewConfigPushMes.setViewName("安全许可证信息");
                List<ViewConfigPushMes> configPushMes = viewConfigPushMesMapper.queryByViewKey(viewConfigPushMes);
                if (!CollectionUtils.isEmpty(configPushMes)) {
                    ViewConfigPushMes pushMes = configPushMes.stream().findFirst().orElse(new ViewConfigPushMes());
                    if (com.ruoyi.common.utils.StringUtils.isNotEmpty(pushMes.getViewValue())) {
                        boolean contains = pushMes.getViewValue().contains(",");
                        if (contains) {
                            String[] split = pushMes.getViewValue().split(",");
                            for (String vars : split) {
                                sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(vars));
                            }
                        } else {
                            sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(pushMes.getViewValue()));
                        }
                    }
                }*/

                SysRole sysRole = new SysRole();
                sysRole.setRoleKey("qyzzts");
                List<SysUser> sysUsers = sysUserMapper.selectUserByRoleName(sysRole);
                if (CollectionUtils.isEmpty(sysUsers)) {
                    return;
                }

                for (SysUser sys : sysUsers) {
                    sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, sys.getUserId().intValue());
                }

                List<String> collect = sysUsers.stream().map(SysUser::getWxUserId).collect(Collectors.toList());
                EnterpriseWXServiceImpl.SendMessageRequest sendMessageRequest = new EnterpriseWXServiceImpl.SendMessageRequest();
                sendMessageRequest.setTouser(CollUtil.join(collect, "|"));
                sendMessageRequest.setMsgtype("text");
                sendMessageRequest.setAgentid(1000319);
                EnterpriseWXServiceImpl.MessageContent messageContent = new EnterpriseWXServiceImpl.MessageContent();
                messageContent.setContent(stringBuffer);
                sendMessageRequest.setText(messageContent);
                enterpriseWXService.pushMessage(sendMessageRequest);
            }
        }
    }


}
